import http from '@ohos.net.http';
import Logger from '../utils/Logger'
export default async function emb(wordlist):Promise<string[]> {
  return new Promise((resolve, reject) => {
    try {
      let httpRequest = http.createHttp();
      const url =
        "https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop/embeddings/bge_large_zh?access_token=24.67ad70c8bd6e5aefb1fe2071dfa32a82.2592000.1741658810.282335-116263096"
      const options = {
        method: http.RequestMethod.POST,
        header: [{
          'Content-Type': 'application/json'
        }],
        extraData: JSON.stringify({
          "input": wordlist
        })
      };
      console.info("_:_[embed] send", JSON.stringify(options), url)
      httpRequest.request(url, options, (err: Error, data: http.HttpResponse) => {
        console.info("_:_[embed] raw result " + data.result)
        let jsonresult = JSON.parse(data.result as string)
        let realresults = []
        for (let data of jsonresult.data) {
          let realresult = "[" + (data.embedding).toString() + "]"
          realresults.push(realresult)
        }
        console.info('_:_[embed] result' + typeof realresults + "||" + realresults[0]);
        resolve(realresults)
      })
    } catch (e) {
      console.log("_:_[embed] error", e)
      resolve(['0'])
    }
  })
}
